#include <stdio.h>

/* 升序冒泡排序：原地交换 */
void bubble_sort(int a[], int n)
{
    for (int i = 0; i < n - 1; ++i) {          /* 共 n-1 趟 */
        // int swapped = 0;                       /* 提前退出优化 */
        for (int j = 0; j < n - 1 - i; ++j)    /* 每趟把最大元素“冒”到末尾 */
            if (a[j] > a[j + 1]) {
                int tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp;
                // swapped = 1;
            }
        // if (!swapped) break;                   /* 已整体有序 */
    }
}

int main(void)
{
    int a[] = {42, 7, 13, 5, 99, 3, 5 ,25 ,69 ,32 , 75};
    int n = sizeof(a) / sizeof(a[0]);

    printf("Before: ");
    for (int i = 0; i < n; ++i) printf("%d ", a[i]);
    printf("\n");

    bubble_sort(a, n);

    printf("After : ");
    for (int i = 0; i < n; ++i) printf("%d ", a[i]);
    printf("\n");

    return 0;
}